package com.iwayin.core.common;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.beanutils.BeanUtils;

/**
 * 
 * @author xiaobo.feng<xiaobo.feng@foxmail.com>
 * 
 *         Aug 13, 2011
 */
@SuppressWarnings("unchecked")
public class Constants {
	// 用户默认头像
	public static final String DEFAULT_USER_AVATAR = "http://face.yoasking.com/2012052000212923211.jpg";
	public static final String DEFAULT_USER_AVATAR_ORIGINAL = "http://face.yoasking.com/2012052000212923211.jpg";
	public static final String COOKIE_USERNAME = "username";
	public static final String COOKIE_PASSWORD = "password";
	public static final String ASK_IMG_DOMAIN = "http://image.yoasking.com/";
	public static final String ASK_IMG_BUCKET = "question-image";
	public static final String ASK_IMG_USERNAME = "yoasking";
	public static final String ASK_IMG_PASSWORD = "xiaobo612";

	public static final String DEFAULT_SYSTEM_ERROR_MSG = "系统繁忙，请稍待片刻再试";
	public static final String SYSTEM_EMAIL_SHOW_NAME = "优问网";

	// 发送邮件配置
	public static String SYSTEM_EMAIL_HOST = "smtp.qq.com";
	public static String SYSTEM_EMAIL_USERNAME = "16278610@qq.com";
	public static String SYSTEM_EMAIL_PASSWORD = "xiaobo.feng";
	/** 邮件中连接是否有效 */
	public static Map<String, String> statusMap = new ConcurrentHashMap<String, String>();

	// 统计信息配置
	public static final Long USER_TOP_SIZE = 6L;

	static {
		getPropertyValue("/constants.properties");// 从constans.properties读取value，将常量重新赋值
	}

	@SuppressWarnings("rawtypes")
	public static void getPropertyValue(String fileName) {
		try {
			Properties props = new Properties();
			InputStream is = Constants.class.getResourceAsStream(fileName);
			Reader reader = new InputStreamReader(is, "UTF-8");
			props.load(reader);
			Map map = new HashMap();
			Constants constants = new Constants();
			Enumeration enums = props.propertyNames();
			while (enums.hasMoreElements()) {
				String key = (String) enums.nextElement();
				map.put(key, props.getProperty(key.toUpperCase()));
			}
			BeanUtils.copyProperties(constants, map);
			is.close();
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setSYSTEM_EMAIL_HOST(String SYSTEM_EMAIL_HOST) {
		Constants.SYSTEM_EMAIL_HOST = SYSTEM_EMAIL_HOST;
	}

	public void setSYSTEM_EMAIL_USERNAME(String SYSTEM_EMAIL_USERNAME) {
		Constants.SYSTEM_EMAIL_USERNAME = SYSTEM_EMAIL_USERNAME;
	}

	public void setSYSTEM_EMAIL_PASSWORD(String SYSTEM_EMAIL_PASSWORD) {
		Constants.SYSTEM_EMAIL_PASSWORD = SYSTEM_EMAIL_PASSWORD;
	}

}
